package org.raycloud.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.raycloud.Excption.CheckCodeExcption;
import org.raycloud.Excption.XHRTimeoutException;
import org.raycloud.commons.Tools;
import org.raycloud.commons.count2rank;
import org.raycloud.service.IGetUserInfos;
import org.raycloud.vo.rateListDetail;
import org.raycloud.vo.shop_rate_list;

import java.util.List;

/**
 * Created by Administrator on 2014/4/4.
 * Time: 9:55
 */
public class NormalSalesGetInfos implements IGetUserInfos {
    shop_rate_list list;

    public NormalSalesGetInfos(shop_rate_list list) {
        this.list = list;
    }

    @Override
    public void parserUserInfo(Document doc) {
        // 开始 抽取数据
        // 淘宝账号
        Elements ele1 = doc.select("div.bd>div.info-block>div.title>a");
        System.out.println(ele1.text());
        //卖家信用
        Elements ele3 = doc.select("li:contains(卖家信用)");
        System.out.println(Tools.getNumbers(ele3.text()));
        //卖家等级
        System.out.println(count2rank.getRank(Tools.getNumbers(ele3.text())));
        // 买家信用
        Elements ele4 = doc.select("li:contains(买家信用)");
        System.out.println(Tools.getNumbers(ele4.text()));
        // 买家等级
        System.out.println(count2rank.getRank(Tools.getNumbers(ele4.text())));
        // 描述相符
        Elements ele5 = doc.select("em");
        System.out.println(ele5);
        // 所在地区
        Elements ele2 = doc.select("div.bd>div.info-block>ul>li:contains(所在地区)");
        System.out.println(ele2.text());

    }

    @Override
    public boolean parserRateHistoryInfo0(Document doc) {
        return false;
    }

    @Override
    public boolean parserRateHistoryInfo1(Document doc) {
        return false;
    }

    @Override
    public boolean parserRateHistoryInfo3(Document doc) {
        //判断当前页面是否有数据
        Elements ele = doc.select("table.show-rate-table>tbody>tr");
        if (ele.size() == 0) {
            System.out.println("数据为空  ");
            return false;
        }

        //开始搜集
        for (int i = 0; i < ele.size(); i++) {
            rateListDetail detail = new rateListDetail();
            System.out.println(ele.get(i).toString());
            detail.setHtmlstr(ele.get(i).toString());
            list.getList().add(detail);

        }
        System.out.println("历史数据容量：" + list.getList().size());
        return true;
    }

    @Override
    public boolean parserXHRinfo0(String str) {
        return false;
    }

    @Override
    public boolean parserXHRinfo1(String str) {
        return false;
    }

    @Override
    public boolean parserXHRinfo3(String str) throws CheckCodeExcption, XHRTimeoutException {
        JSONObject jsonobj = JSONObject.parseObject(str);

        //判断是否出验证码
        if (jsonobj.get("status") != null && jsonobj.get("status").toString().equals("checkcode")) {
            throw new CheckCodeExcption(jsonobj.getJSONObject("result").get("codeimg").toString());
//            CheckCodeSubmit.submit(jsonobj.getJSONObject("result").get("codeimg").toString(),jsonobj.getJSONObject("result").get("checkurl").toString(),jsonobj.getJSONObject("result").get("sessionID").toString());
        }

        if (jsonobj.get("status") != null && jsonobj.get("status").toString().equals("1111"))
        {
            throw new XHRTimeoutException();
        }

        //判断是否存在数据解析
        JSONArray ratelistdetail = jsonobj.getJSONArray("rateListDetail");
        System.out.println("数据容量" + ratelistdetail.size());
        if (ratelistdetail.size() == 0) return false;

        //没有验证码同时存在数据容量，解析
        list.setCurrentPageNum(Integer.parseInt(jsonobj.get("currentPageNum").toString()));
        list.setMaxPage(Integer.parseInt(jsonobj.get("maxPage").toString()));
        list.setWatershed(Integer.parseInt(jsonobj.get("watershed").toString()));
        List<rateListDetail> list2 = list.getList();
        for (int i = 0; i < ratelistdetail.size(); i++) {
            rateListDetail detail = new rateListDetail();
            detail.setJsonstr(ratelistdetail.get(i).toString());
            list2.add(detail);
        }
        list.setList(list2);
        return true;
    }
}
